package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.lifecycle.CompositeGeneratedAdaptersObserver;
import androidx.lifecycle.FullLifecycleObserverAdapter;
import androidx.lifecycle.ReflectiveGenericLifecycleObserver;
import androidx.lifecycle.SingleGeneratedAdapterObserver;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestrictTo({d.ॱ})
public class ku {
    private static Map<Class, List<Constructor<? extends ko>>> ˊ = new HashMap();
    private static Map<Class, Integer> ॱ = new HashMap();

    @NonNull
    static kq ॱ(Object obj) {
        boolean z = obj instanceof kq;
        boolean z2 = obj instanceof kr;
        if (z && z2) {
            return new FullLifecycleObserverAdapter((kr) obj, (kq) obj);
        }
        if (z2) {
            return new FullLifecycleObserverAdapter((kr) obj, null);
        }
        if (z) {
            return (kq) obj;
        }
        Class cls = obj.getClass();
        if (ˎ(cls) != 2) {
            return new ReflectiveGenericLifecycleObserver(obj);
        }
        List list = (List) ˊ.get(cls);
        if (list.size() == 1) {
            return new SingleGeneratedAdapterObserver(ॱ((Constructor) list.get(0), obj));
        }
        ko[] koVarArr = new ko[list.size()];
        for (int i = 0; i < list.size(); i++) {
            koVarArr[i] = ॱ((Constructor) list.get(i), obj);
        }
        return new CompositeGeneratedAdaptersObserver(koVarArr);
    }

    private static ko ॱ(Constructor<? extends ko> constructor, Object obj) {
        try {
            return (ko) constructor.newInstance(new Object[]{obj});
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } catch (Throwable e2) {
            throw new RuntimeException(e2);
        } catch (Throwable e22) {
            throw new RuntimeException(e22);
        }
    }

    @Nullable
    private static Constructor<? extends ko> ˏ(Class<?> cls) {
        try {
            Package packageR = cls.getPackage();
            String canonicalName = cls.getCanonicalName();
            String name = packageR != null ? packageR.getName() : "";
            if (!name.isEmpty()) {
                canonicalName = canonicalName.substring(name.length() + 1);
            }
            canonicalName = ˎ(canonicalName);
            if (!name.isEmpty()) {
                canonicalName = name + "." + canonicalName;
            }
            Constructor<? extends ko> declaredConstructor = Class.forName(canonicalName).getDeclaredConstructor(new Class[]{cls});
            if (declaredConstructor.isAccessible()) {
                return declaredConstructor;
            }
            declaredConstructor.setAccessible(true);
            return declaredConstructor;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (Throwable e2) {
            throw new RuntimeException(e2);
        }
    }

    private static int ˎ(Class<?> cls) {
        Integer num = (Integer) ॱ.get(cls);
        if (num != null) {
            return num.intValue();
        }
        int ˋ = ˋ(cls);
        ॱ.put(cls, Integer.valueOf(ˋ));
        return ˋ;
    }

    private static int ˋ(Class<?> cls) {
        if (cls.getCanonicalName() == null) {
            return 1;
        }
        Constructor ˏ = ˏ(cls);
        if (ˏ != null) {
            ˊ.put(cls, Collections.singletonList(ˏ));
            return 2;
        } else if (kl.ˋ.ॱ(cls)) {
            return 1;
        } else {
            Class superclass = cls.getSuperclass();
            List list = null;
            if (ˊ(superclass)) {
                if (ˎ(superclass) == 1) {
                    return 1;
                }
                list = new ArrayList((Collection) ˊ.get(superclass));
            }
            for (Class cls2 : cls.getInterfaces()) {
                if (ˊ(cls2)) {
                    if (ˎ(cls2) == 1) {
                        return 1;
                    }
                    List arrayList;
                    if (list == null) {
                        arrayList = new ArrayList();
                    } else {
                        arrayList = list;
                    }
                    arrayList.addAll((Collection) ˊ.get(cls2));
                    list = arrayList;
                }
            }
            if (list == null) {
                return 1;
            }
            ˊ.put(cls, list);
            return 2;
        }
    }

    private static boolean ˊ(Class<?> cls) {
        return cls != null && kt.class.isAssignableFrom(cls);
    }

    public static String ˎ(String str) {
        return str.replace(".", "_") + "_LifecycleAdapter";
    }
}
